<?php
/**
 * Created by PhpStorm.
 * User: zhangjian37
 * Date: 2016/12/6
 * Time: 17:00
 */

$RootDir = $_SERVER['DOCUMENT_ROOT'];
include $RootDir.'/utils/mysql/MysqlUtils.php';

$error = 0;
$error_content = "test";

$type = $_POST['type'];

//先处理一下图片文件
$image_url = "";
if($_FILES['imgurl']['type'] == 'image/gif'
    ||$_FILES['imgurl']['type'] == 'image/jpeg'
    ||$_FILES['imgurl']['type'] == 'image/pjpeg'
    ||$_FILES['imgurl']['type'] == 'image/x-png'){
    if($_FILES['imgurl']['error'] > 0){
        $error = 1;
        $error_content = "上传图片不对 Code: " . $_FILES['imgurl']['error'];
    }else{
        //新文件
        $imgnameFile = fopen("imgname_file","w");
        $file_index = (int)fgets($imgnameFile);
        $file_index += 1;
        while (file_exists("upload/" . $file_index . "_" . $_FILES['imgurl']['name'])){
            $file_index += 1;
        }
        $image_url = "upload/" . $file_index . "_" . $_FILES['imgurl']['name'];
        move_uploaded_file($_FILES['imgurl']['tmp_name'],$image_url);
        fwrite($imgnameFile,$file_index);
        fclose($imgnameFile);
    }
}

$conn = conn_mysql("youhuikuaibao");
$query = "";
if($type == 'baicai'){
    $title = $_POST['title'];
    $news_price = $_POST['news_price'];
    $content = $_POST['content'];
    $url = $_POST['url'];
    $query = "insert into baicai(title,news_price,content,image_url,url) VALUES
               ('$title','$news_price','$content','$image_url','$url') ";
}else if($type == 'free'){
    $title = $_POST['title'];
    $org_price = $_POST['org_price'];
    $copies = $_POST['copies'];
    $url = $_POST['url'];
    $query = "insert into free(title,org_price,copies,image_url,url) VALUES
               ('$title','$org_price','$copies','$image_url','$url') ";
}else if($type == 'mission'){
    $title = $_POST['title'];
    $mission_money = $_POST['mission_money'];
    $pay_money = $_POST['pay_money'];
    $return_money = $_POST['return_money'];
    $copies = $_POST['copies'];
    $end_time = $_POST['end_time'];
    $url = $_POST['url'];
    $query = "insert into mission(title,mission_money,pay_money,return_money,copies,end_time,image_url,url) VALUES
               ('$title','$mission_money','$pay_money','$return_money','$copies,end_time','$image_url','$url') ";
}

if (!mysql_query($query, $conn)) {

}else{
    $error = 1;
    $error_content = "数据保存失败: ";
}

if($error == 1){
    echo "<script type=text/javascript>";
    echo 'window.open(\'../html/manage.html?type='.$type.'&error='.$error_content.'\',\'_self\');';
    echo "</script>";
}else {
    //调整到主页
    echo "<script type=text/javascript>";
    echo 'window.open(\'../html/jufuli.html\',\'_self\');';
    echo "</script>";
}